﻿using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;

namespace MDCAdminTool.Controls.Cache_Cluster
{
	/// <summary>
	/// Interaction logic for ManageSecurity.xaml
	/// </summary>
	public partial class ManageSecurity : UserControl
	{
		public CacheCluster Cluster { get; private set; }

		public ManageSecurity(CacheCluster cluster)
		{
			InitializeComponent();
			Cluster = cluster;

			Accounts = new ObservableCollection<String>();
			var accounts = Cluster.GetAllowedAccounts();
			foreach (var account in accounts)
			{
				Accounts.Add(account);
			}
			dgAccounts.ItemsSource = Accounts;
		}

		public ObservableCollection<String> Accounts { get; set; }

		private void OnRowDelete(object sender, RoutedEventArgs e)
		{
			if (dgAccounts.SelectedIndex >= 0)
			{
				//remove the selectedItem from the collection source
				var val  = dgAccounts.SelectedItem as String;
				Accounts.Remove(val);
				Cluster.RevokeAllowedAccount(val);
			}
		}

		private void OnItemAdd(object sender, RoutedEventArgs e)
		{
			var value = txtNew.Text;
			
			if(!String.IsNullOrEmpty(value))
			{
				Cluster.GrantAllowedAccount(value);
				Accounts.Add(value);
			}
		}
	}
}
